package com.jackrain.nea.fc.vp.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * FC-会员接口-VIP收货地址
 * Mapper
 *
 * @author WSL
 * @create 2017/11/13
 */
@Mapper
public interface ViewCvipAddrMapper {
    class CvipAddrInsertSql {
        public String insertCvipAddr(JSONObject jsonObject) {
            return new SQL() {
                {
                    INSERT_INTO("VP_C_VIP_ADDR");
                    Set<String> keySet = jsonObject.keySet();
                    for (String key : keySet) {
                        VALUES(key, "#{" + key + "}");
                    }
                }
            }.toString();
        }
    }

    /**
     * 新增
     *
     * @param jsonObject
     * @return
     */
    @InsertProvider(type = CvipAddrInsertSql.class,
            method = "insertCvipAddr")
    int insert(JSONObject jsonObject);

    class CvipAddrUpdateSql {
        public String updateCvipAddr(final JSONObject jsonObject) {
            return new SQL() {
                {
                    UPDATE("VP_C_VIP_ADDR");
                    Set<String> keySet = jsonObject.keySet();
                    for (String key : keySet) {
                        if (!"ID".equalsIgnoreCase(key)
                                &&!"VP_C_VIP_ID".equalsIgnoreCase(key)
                                && !"LINE".equalsIgnoreCase(key)) {
                            SET(key + "=#{" + key + "}");
                        }
                    }
                    WHERE("VP_C_VIP_ID=#{VP_C_VIP_ID}");
                    WHERE("LINE=#{LINE}");
                }
            }.toString();
        }
    }

    /**
     * 更新
     *
     * @param jsonObject
     * @return int
     */
    @UpdateProvider(type = CvipAddrUpdateSql.class,
            method = "updateCvipAddr")
    int update(JSONObject jsonObject);

    /**
     * VIP:根据ECODE查找【VIP类型】表中ID
     *
     * @param cVipTypeGroupId
     * @return
     */
    @Select("SELECT ID FROM VP_C_VIPTYPEGROUP WHERE ECODE = #{cVipTypeGroupId}")
    Long getCvipTypeGroupId(String cVipTypeGroupId);

    /**
     * 获取USERID
     *
     * @param name
     * @return
     */
    @Select("SELECT ID FROM USERS WHERE `NAME` = #{name}")
    Long getUserId(String name);

    /**
     * 通过VIPID和序号查询唯一数据
     *
     * @param vipId
     * @param line
     * @return
     */
    @Select("SELECT COUNT(1) FROM VP_C_VIP_ADDR WHERE VP_C_VIP_ID = #{arg0} AND LINE = #{arg1}")
    int check(Long vipId, Long line);

    class SelectPro {
        public String selectProvider(Map map) {
            return new SQL() {
                {
                    SELECT("VP_C_VIP_ID,LINE,RECEIVER,MOBIL,CP_C_PRO_ID,CP_C_CITY_ID,CP_C_DIST_ID,ADDRESS,ISDEFAULT," +
                            "OWNERID,CREATIONDATE,MODIFIERID,MODIFIEDDATE");
                    FROM("VP_C_VIP_ADDR");
                    Object[] list = (Object[]) map.get("list");
                    StringBuilder sb = new StringBuilder("(");
                    for (Object num : list) {
                        sb.append(num).append(",");
                    }
                    sb.deleteCharAt(sb.length() - 1).append(")");
                    WHERE("ID IN " + sb.toString());
                }
            }.toString();
        }
    }

    /**
     * 根据param查询条件 查询【VIP收货地址】表
     *
     * @param param
     * @return String
     */
    @SelectProvider(type = SelectPro.class, method = "selectProvider")
    List<HashMap> selectProvider(@Param("list") Object[] param);


    // FC ——》 F4

    /**
     * 【VIP档案ID】+【VIP类型】查找到对应【VIP账户】数据行中的【VIP卡号】
     *
     * @param vipId
     * @return
     */
    @Select("SELECT a.ECODE FROM VP_C_VIP_ACC a INNER JOIN VP_C_VIPTYPEGROUP t ON t.ID = a.VP_C_VIPTYPEGROUP_ID WHERE a.VP_C_VIP_ID = #{vipId} AND t.ECODE = 'FF';")
    String selVipAccEcode(Long vipId);

}
